{include file="header.tpl"}
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
	
$(document).ready(function()
{
  $(".delete").live('click', function(event) 
  {
    $(this).parent().remove();
    setTotAmount();
  });
});

function addNewRow()
{
  $('#billBook tr:last').after('<tr class="itemRow"><td align="center"><input type="text" name="particulars[]" \/><\/td><td align="center"><input type="text" name="kg[]" class="kg" onChange="setAmount(this)"\/><\/td><td align="center"><input type="text" name="bhav[]" class="bhav" onChange="setAmount(this)"\/><\/td><td align="center"><input type="text" name="cramount[]" class="cramount" onChange="setTotAmount(this)"\/><\/td><td align="center"><input type="text" name="crfine[]" class="crfine" onChange="setTotAmount(this)" \/><\/td><td align="center"><input type="text" name="dramount[]" class="dramount" onChange="setTotAmount(this)" \/><\/td><td align="center"><a href="javascript:addNewRow();">{$l_add}<\/a><\/td><td class="delete">{$l_remove}<\/td><\/tr>');
}		

function setAmount(txtObj)
{
  var row = $(txtObj).parents('.itemRow');
  var price = parseFloat(row.find('.kg').val().replace("$","")) * parseFloat(row.find('.bhav').val()/1000);
  isNaN(price) ? row.find('.cramount').val("") : row.find('.cramount').val(price);
  setTotAmount();
}

function setTotAmount()
{
  var totalkg       = 0;
  var totalcramount = 0;
  var totaldramount = 0;
  var totcrFine = 0;
  
  $('.kg').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalkg += Number(price);
  });
  
  $('.cramount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totalcramount += Number(price);
  });
  
  $('.dramount').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totaldramount += Number(price);
  });
  
 $('.crfine').each(function(i)
  {
    price = $(this).val().replace("$","");
    if (!isNaN(price)) totcrFine += Number(price);
  });

  $('#totkg').val(totalkg);
  $('#totcramount').val(totalcramount);
  $('#totdramount').val(totaldramount);
  $('#totcrFine').val(totcrFine);
}
function getOldAmount()
{
  if(document.form1.party.value > 0)
	{
	  $.ajax({
	  type:"POST",
	  url:'getOldAmountBullion.php',
	  data:
	  {
	    partyId : document.form1.party.value
	  },
	  success: function(response)
	  {
	  	var temp = response.split(",");
	  	prevPartyFine = temp[0];
	  	prevPartyAmount = temp[1];
	  	$('#partyFine').val(prevPartyFine);
	  	$('#partyAmount').val(prevPartyAmount);
	  }
	  });
	}
}
</script>
<form name="form1" action="" method="post">
<input type="hidden" name="isEdit" value="{$isEdit}" />
<input type="hidden" name="bullionmasterid" value="{$bullionmasterid}" />

<center><h4>Bullion Entry</h4></center>
{if $msg != ''}<center><span style="color:#FF0000"><b>{$msg}</b></span></center>{/if}
<div class="contentDiv">
  <table border="1  " id="billBook" align="center" width="100%" cellpadding="0">
  <tr>
    <td colspan="5" class="table1">
      {$l_partySelect} :
      <select name="party" onChange="getOldAmount();">
        {html_options values=$party.partyId output=$party.bullionPartyName selected=$partyId}
      </select>
    </td>
    <td colspan="6" class="table1">
      {$l_bulliondate} :
      {html_select_date prefix="curDate" start_year="-2" end_year="+1" field_order="DMY" day_value_format="%02d" time=$bulliondate}
    </td>
  </tr>
  <tr class="table1">
    <th>{$l_particulars}</th>
    <th>{$l_kg}</th>
    <th>{$l_bhav}</th>
    <th>{$l_cramount}</th>
    <th>{$l_crfine}</th>
    <th>{$l_dramount}</th>
    <th colspan="2">&nbsp;</th>
  </tr>
  {if $isEdit == 1}
    {section name="sec" loop=$bullionArray|@count}
      <tr class="itemRow">
        <td align="center"><input type="text" name="particulars[]" value="{$bullionArray[sec]['particulars']}" /></td>
        <td align="center"><input type="text" name="kg[]" class="kg" onChange="setAmount(this)"  value="{$bullionArray[sec]['kg']}" /></td>
        <td align="center"><input type="text" name="bhav[]" class="bhav" onChange="setAmount(this)" value="{$bullionArray[sec]['bhav']}" /></td>
        <td align="center"><input type="text" name="cramount[]" class="cramount" value="{$bullionArray[sec]['cramount']}"/></td>
        <td align="center"><input type="text" name="crfine[]" class="crfine" onChange="setTotAmount(this)" value="{$bullionArray[sec]['crfine']}" /></td>
        <td align="center"><input type="text" name="dramount[]" class="dramount" onChange="setTotAmount(this)" value="{$bullionArray[sec]['dramount']}" /></td>
        <td align="center"><a href="javascript:addNewRow();">{$l_add}</a></td>
        <td class="delete">{$l_remove}</td>
      </tr>
    {/section}
  {/if}
  <tr class="itemRow">	
    <td align="center"><input type="text" name="particulars[]" /></td>
    <td align="center"><input type="text" name="kg[]" class="kg" onChange="setAmount(this)"/></td>
    <td align="center"><input type="text" name="bhav[]" class="bhav" onChange="setAmount(this)" /></td>
    <td align="center"><input type="text" name="cramount[]" class="cramount" onChange="setTotAmount(this)"/></td>
    <td align="center"><input type="text" name="crfine[]" class="crfine" onChange="setTotAmount(this)"/></td>
    <td align="center"><input type="text" name="dramount[]" class="dramount"  onChange="setTotAmount(this)" /></td>
    <td align="center"><a href="javascript:addNewRow();">{$l_add}</a></td>
    <td align="center">&nbsp;</td>
  </tr>
  </table>
  
  <table border="1" width="100%">
    <tr class="itemRowTot">
      <th align="right"  width="145px;">{$l_total}</th>
      <th align="center" width="145px;"><input type="text" name="totkg" id="totkg"/></th>
      <th align="right"  width="145px;">&nbsp;</th>
      <th align="center"  width="146px;"><input type="text" name="totcramount" id="totcramount"/></th>
      <th align="center"  width="146px;"><input type="text" name="totcrFine"   id="totcrFine"/></th>
      <th align="right"  width="180px;"><input type="text" name="totdramount" id="totdramount"/></th>
      <th align="right"  width="80px;">&nbsp;</th>
      <th colspan="2"    width="110px;"></th>
    </tr>
  </table>
   <div align="right">
  	 Previous Fine :   <input type ="text" name="Netfine" id="partyFine" onChange="setTotAmount"><br>	
  	 Previous Amount : <input type ="text" name="Netamount" id="partyAmount" onChange="setTotAmount">
   </div>
  <div align="left">
  {if $isEdit == 1}
    <input type="submit" name="billBookAddBtn" value="{$l_save}"/>
  {else}
    <input type="submit" name="billBookAddBtn" value="{$l_addBillBook}"/>
  {/if}
</div>
</form>
<script type="text/javascript">
{if $isEdit == 1}
  setTotAmount();
{/if}  
</script>
{include file="footer.tpl"}